package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzTransfer;
import com.gpframework.module.wages.domain.req.GzTransferAdd;
import com.gpframework.module.wages.domain.req.GzTransferReq;
import com.gpframework.module.wages.domain.req.GzTransferUpdate;
import com.gpframework.module.wages.domain.vo.GzTransferVo;
import com.gpframework.module.wages.service.IGzTransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzTransferController
 * @Author gpframework
 * @Date 2024-01-22
 * @Version 1.0.0
 * @Description 中转人员工资Controller
 */
@Validated
@Api(tags = "中转人员工资")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/transfer" )
public class GzTransferController extends BaseController {

    private final IGzTransferService iGzTransferService;


 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-22
  * @Param gzTransfer
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo<GzTransferVo> list(GzTransferReq gzTransferReq)
    {
        startPage();
        QueryWrapper<GzTransfer> lqw = new QueryWrapper<GzTransfer>();
        gzTransferReq.generatorQuery(lqw,true);
        List<GzTransferVo> list = iGzTransferService.listVo(lqw);
        return getDataTable(list);
    }


    @ApiOperation("列表统计")
    @PreAuthorize("@ss.hasPermi('wages:transfer:list')")
    @GetMapping("/listStatistics")
    public TableDataInfo<GzTransferVo> listStatistics(GzTransferReq gzTransferReq)
    {
        startPage();
        QueryWrapper<GzTransfer> lqw = new QueryWrapper<GzTransfer>();
        gzTransferReq.generatorQuery(lqw,true);
        List<GzTransferVo> list = iGzTransferService.listStatistics(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzTransfer
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:transfer:add')" )
    @Log(title = "添加中转人员工资" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzTransferAdd gzTransferAdd) {
        GzTransfer gzTransfer = Convert.convert(new TypeReference<GzTransfer>() {}, gzTransferAdd);
        return toAjax(iGzTransferService.save(gzTransfer) ? 1 : 0);
    }

    @ApiOperation("是否上报审批")
    @PreAuthorize("@ss.hasPermi('wages:transfer:edit')" )
    @Log(title = "是否上报审批" , businessType = BusinessType.UPDATE)
    @PutMapping("/peported")
    public AjaxResult peported(@Valid @RequestBody GzTransferUpdate gzTransferUpdate) {
        List<GzTransfer> gzTransferList = gzTransferUpdate.getGzTransferList();
        for (GzTransfer gzTransfer : gzTransferList) {
            gzTransfer.setEscalation(Long.valueOf("1"));
            iGzTransferService.updateById(gzTransfer);
        }
        return AjaxResult.success();
    }

    /**
     * @Description 审批修改
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzTransfer
     * @Return TableDataInfo
     */
    @ApiOperation("审批修改")
    @PreAuthorize("@ss.hasPermi('wages:transfer:edit')" )
    @Log(title = "审批修改中转人员工资" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzTransferUpdate gzTransferUpdates) {
        for (GzTransfer gzTransfer : gzTransferUpdates.getGzTransferList()) {
            if (Long.valueOf("1").equals(gzTransfer.getEscalation())){
                GzTransfer transferUpdate = Convert.convert(new TypeReference<GzTransfer>() {}, gzTransfer);
                iGzTransferService.updateById(transferUpdate);
            }else {
                return AjaxResult.error("请选择已经审核的人员");
            }
        }
        return AjaxResult.success();
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzTransfer
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:transfer:export')" )
    @Log(title = "导出中转人员工资" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzTransferReq gzTransferReq) {
        QueryWrapper<GzTransfer> lqw = new QueryWrapper<GzTransfer>();
        gzTransferReq.generatorQuery(lqw,true);
        List<GzTransferVo> list = iGzTransferService.listVo(lqw);
        ExcelUtil<GzTransferVo> util = new ExcelUtil<GzTransferVo>(GzTransferVo. class);
        return util.exportExcel(list, "transfer" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzTransfer
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:transfer:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzTransferVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzTransferService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzTransfer
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:transfer:remove')" )
    @Log(title = "删除中转人员工资" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzTransferService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
